package com.qdlc.p2p.biz.sms;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.HttpSender;
import com.qdlc.p2p.common.util.HttpUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.SystemConfigHelper.Nid;
import com.qdlc.p2p.dal.dao.SystemConfigDao;

public class ChuangLanSmsPortalImpl implements SmsPortal {
	private Logger logger=Logger.getLogger(ChuangLanSmsPortalImpl.class);
	
	@Override
	public String send(String phone, String content) {
		SystemConfigDao systemConfigDao = (SystemConfigDao) BeanUtil.getBean("systemConfigDao");
		String username = systemConfigDao.findObjByProperty("nid", "con_sms_username").getValue();
		String password = systemConfigDao.findObjByProperty("nid", "con_sms_password").getValue();
		String smsUrl = systemConfigDao.findObjByProperty("nid", "con_sms_notice").getValue();
		String returnString = "";
		try {
			returnString = HttpSender.batchSend(smsUrl, username, password, phone, content, true, null, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String ss = "";
		if (returnString.indexOf("\n") == -1) {
			ss = returnString.substring(returnString.indexOf(",")+1, returnString.length());
		} else {
			ss = returnString.substring(returnString.indexOf(",")+1, returnString.indexOf("\n"));
		}
		int ret = StringUtil.toInt(StringUtil.isNull(ss));
		String result = "ok";

		if (ret != 0){
			switch (ret) {
			case 101:
				result = "无此商户账号";
				break;
			case 102:
				result = "商户账号密码错误";
				break;
			case 103:
				result = "提交过快（提交速度超过流速限制）";
				break;
			case 104:
				result = "系统忙（因平台侧原因，暂时无法处理提交的短信）";
				break;
			case 105:
				result = "敏感短信（短信内容包含敏感词）";
				break;
			case 106:
				result = "消息长度错（>536或<=0）";
				break;
			case 107:
				result = "包含错误的手机号码";
				break;
			case 108:
				result = "手机号码个数错（群发>50000或<=0;单发>200或<=0）";
				break;
			case 109:
				result = "无发送额度（该用户可用短信数已使用完）";
				break;
			case 110:
				result = "不在发送时间内";
				break;
			case 111:
				result = "超出该账户当月发送额度限制";
				break;
			case 112:
				result = "无此产品，用户没有订购该产品";
				break;
			case 113:
				result = "extno格式错（非数字或者长度不对）";
				break;
			case 115:
				result = "自动审核驳回";
				break;
			case 116:
				result = "签名不合法，未带签名（用户必须带签名的前提下）";
				break;
			case 117:
				result = "IP地址认证错,请求调用的IP地址不是系统登记的IP地址";
				break;
			case 118:
				result = "用户没有相应的发送权限";
				break;
			case 119:
				result = "用户已过期";
				break;
			default:
				result = "其他异常错误";
				break;
			}
			logger.info(phone+"：发送短信失败，失败原因："+result);
		}
		return result;
	}

	@Override
	public String getSPName() {
		return "Chuanglan";
	}
}
